/****************************************************************************
 * 2025.5 KS
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class AchivementController : UIElement
	{
		ResLoader mResLoader=ResLoader.Allocate();
		private void Awake()
		{
			var originLocalPosY=AchivementItem.LocalPositionY();
			var iconAtlas=mResLoader.LoadSync<SpriteAtlas>("Icon");
		    // 注册成就事件
			AchievementSystem.OnAchievementUnlocked.Register(item=>
			{
				Title.text=$"<b>成就{item.Name} 达成!</b>";
				Description.text=item.Description;
				var sprite=iconAtlas.GetSprite(item.IconName);
				Image.sprite=sprite;
				AchivementItem.Show();
				AchivementItem.LocalPositionY(-200);
				AudioKit.PlaySound("Achievement");
				// 动画效果
				ActionKit.Sequence()
					 .Lerp(-200, originLocalPosY, 0.3f, (y) => AchivementItem.LocalPositionY(y))
					 .Delay(2)
					 .Lerp(originLocalPosY, -200, 0.3f, (y) => AchivementItem.LocalPositionY(y), () =>
					 {
						 AchivementItem.Hide();
					 })
					 .Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

		}
		/// <summary>
		/// 销毁前
		/// </summary>
		protected override void OnBeforeDestroy()
		{
			// 回收资源
			mResLoader.Recycle2Cache();
			mResLoader=null;
		}
	}
}